let url = 'http://127.0.0.1:8000/';
let getstatus = 'get', poststatus = 'post';
let getarticleinfo = url + 'getArticleInfo';
let getsoftdeleteinfo = url + 'getsoftDeleteInfo';
let url_status = false;
let search_input, temp, status_temp;
let count = 10;//一页中共有多少数据

$(function () {

    request_data(GetThisUrl(), getstatus, true);

    $(document).on('change', '.across', function () {
        url_status = false;
        request_data(GetThisUrl(), getstatus, true);
    });

    //回收箱的点击事件
    $(document).on('click', '.huishou', function () {
        $('.shaixuan').css('display', 'none');
        $('.across').css('display', 'none');
        $('.search-input').css('display', 'none');
        $('.search').css('display', 'none');
        $('.huishou').css('display', 'none');
        $('.back').css('display', 'inline');
        url_status = false;
        request_data(GetThisUrl(), getstatus, true);
    });

    //返回键点击
    $(document).on('click', '.back', function () {
        $('.shaixuan').css('display', 'inline');
        $('.across').css('display', 'inline');
        $('.search-input').css('display', 'inline');
        $('.search').css('display', 'inline');
        $('.huishou').css('display', 'inline');
        $('.back').css('display', 'none');
        request_data(GetThisUrl(), getstatus, true);
    });

    //页码的点击事件
    $(document).on("click", ".page", function () {
        $(".page").removeClass("active");
        $(this).addClass("active");
        let clickpage = $(this).text();
        request_data(GetThisUrl(true), getstatus, false, clickpage);
        $(".nowpage").text(clickpage);
    });

    //上一页的点击事件
    $(document).on("click", ".last-page", function () {
        let page = $(".active").text();
        pageclick(page, "pre");
    });

    //下一页的点击事件
    $(document).on("click", ".next-page", function () {
        let page = $(".active").text();
        pageclick(page, "next");
    });

    //搜索按钮的点击事件
    $(document).on('click', '.search', function () {
        search_input = $('.search-input').val();
        if (search_input === '') {
            hint('请输入标题搜索文章');
        }
        url_status = true;
        request_data(GetThisUrl(), getstatus, true);
        $('.search-input').val('');
    });

    //删除的点击
    $(document).on('click', '.delete', function () {
        temp = $(this).parent().prev().prev().prev().prev().prev().prev().attr('data-id');
        $(".delete-warning h4").html(`<h4>确认删除此文章？删除的文章将会保存到<span style="color: green;">回收箱</span>中</h4>`);
        status_temp = 1;
        $(".delete-warning").fadeIn(500)
    });

    //彻底删除的点击
    $(document).on('click', '.thoroughly', function () {
        temp = $(this).parent().prev().prev().prev().prev().prev().prev().attr('data-id');
        $(".delete-warning h4").html(`确认彻底删除此文章？将彻底<span style="color: red">删除文章</span>！`);
        status_temp = -1;
        $(".delete-warning").fadeIn(500)
    });

    //恢复的点击
    $(document).on('click', '.recovery', function () {
        temp = $(this).parent().prev().prev().prev().prev().prev().prev().attr('data-id');
        $(".delete-warning h4").html(`确认恢复此文章？此文章将会返回文章列表！`);
        status_temp = 0;
        $(".delete-warning").fadeIn(500)
    });
    //确认删除的点击
    $(document).on('click', '.delete-sure', function () {
        $(".delete-warning").fadeOut(500);
        if (status_temp === 1) {
            softDelete(temp);
        } else if (status_temp === -1) {
            utterDelete(temp);
        } else if (status_temp === 0) {
            Recovr(temp);
        }
    });

    //取消的点击
    $(document).on('click', '.delete-giveup', function () {
        $(".delete-warning").fadeOut(500)
    });

});

function request_data(url, status, pagination, page,) {
    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});//设置访问的默认值
    if (page !== undefined) {
        if (url.substring(0, 30) === (url + 'getSearchArticle?keyword=' + search_input).substring(0, 30))
            url += '&page=' + page;
        else
            url += '?page=' + page;
    }
    $.ajax({
        type: status,
        url: url,
        dataType: "json",
        async: false,//异步操作
        error: function () {
            console.log("失败");
        },
        success: function (data) {
            if (data.code === 100) {
                console.log(data.msg);
            }
            if (data.code === 200) {
                console.log(data);
                pulldata(url, data, pagination);
            }
        }
    });
}

function pulldata(url, data, pagination) {
    $('#data').empty();
    let length = getarticleinfo.length;
    let this_pages = data.data.current_page;
    let $li = '';
    $(data.data.data).each(function (index, item) {
        $li += `<ul>
        <li data-id = "${item.id}">${(index) + (this_pages - 1) * count + 1}</li>
        <li>${item.auth}</li>
        <li>${item.title}</li>
        <li>${item.column}</li>`;
        if (item.status === 0)
            $li += `<li style="color: black;background: green;">已通过</li>`;
        else if (item.status === 1)
            $li += `<li style="background: darkgray;color: black;">待审核</li>`;
        else if (item.status === 2)
            $li += `<li style="background: red;color: black;">未通过审核</li>`;
        if (url.substring(0, length) === getsoftdeleteinfo.substring(0, length)) {
            $li += `<li>${item.created_at}</li>
            <li>
            <button class="recovery" value="${item.id}">恢复</button>
            <button class="thoroughly">彻底删除</button>
            </li>
            </ul>`;
        } else {
            $li += `<li>${item.created_at}</li>
            <li>
            <button class="examine" value="${item.id}">审核</botton>
            <button class="delete">删除</button>
            </li>
            </ul>`;
        }
    });
    $('#data').append($li);
    if (pagination === true) {
        $(".page").remove();
        let pages = data.data.last_page;
        for (let i = 1; i <= pages; i++) {
            let page = `<div class="page pp">${i}</div>`;
            $(".page-box-one>.pp").last().after(page);
        }
        $(".page").eq(0).addClass("active");
        $(".page").css("display", "block");
        $(".page-box-one .pagez").text(pages);
    }
}

function pageclick(page, status) {
    let acount_page = $(".page-box-one .pagez").text();
    let thispage = $(".active");
    let temppage;
    if (status === "next") {
        if (page === (acount_page).toString()) {
            hint("已是最后一页");
            return;
        }
        page++;
        temppage = thispage.next();
    } else if (status === "pre") {
        if (page === '1') {
            hint("已是第一页!");
            return;
        }
        page--;
        temppage = thispage.prev()
    }
    thispage.removeClass("active");
    temppage.addClass("active");
    request_data(GetThisUrl(true), getstatus, false, page);
    $(".page-box-one .nowpage").text(page);
}

/**
 * @return {string}
 */
function GetThisUrl() {
    if (url_status === true) {
        console.log(search_input);
        return (url + 'getSearchArticle?keyword=' + search_input);
        // return getsearcharticle;
    }
    if ($('.huishou').css('display') === 'inline') {
        let num = $('.across').val();
        return getarticleinfo + '/' + num;
    } else return getsoftdeleteinfo;
}


function Pass(id) {//审核通过
    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});//设置访问的默认值
    $.ajax({
        type: 'post',
        url: url + "postPass?number=" + id,
        dataType: "json",
        async: true,//异步操作
        error: function () {
            console.log("失败");
        },
        success: function (data) {
            if (data.code === 100) {
                hint(data.msg);
            }
            if (data.code === 200) {
                hint(data.msg);
            }
        },
    })
}

function NotPass(id, text) {//审核不通过
    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});//设置访问的默认值
    $.ajax({
        type: 'post',
        url: url + "postNotPass?number=" + id + "&text=" + text,
        dataType: "json",
        async: true,//异步操作
        error: function () {
            console.log("失败");
        },
        success: function (data) {
            if (data.code === 100) {
                hint(data.msg);
            }
            if (data.code === 200) {
                hint(data.msg);
            }
        },
    });
    location.reload(true);
}

function softDelete(id) {//软删除
    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});//设置访问的默认值
    $.ajax({
        type: 'post',
        url: url + "postSoftDelete?number=" + id,
        dataType: "json",
        async: true,//异步操作
        error: function () {
            console.log("失败");
        },
        success: function (data) {
            console.log(data);
            if (data.code === 100) {
                hint(data.msg);
            }
            if (data.code === 200) {
                hint(data.msg);
            }
        },
    });
    request_data(GetThisUrl(), getstatus);
}

function utterDelete(id) {//彻底删除
    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});//设置访问的默认值
    $.ajax({
        type: 'post',
        url: url + "postDelete?number=" + id,
        dataType: "json",
        async: true,//异步操作
        success: function (data) {
            if (data.code === 100) {
                hint(data.msg);
            }
            if (data.code === 200) {
                hint(data.msg);
            }
        },
    });
    request_data(GetThisUrl(), getstatus);

}

function Recovr(id) {//恢复
    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});//设置访问的默认值
    $.ajax({
        type: 'post',
        url: url + "postRecovr?number=" + id,
        dataType: "json",
        async: true,//异步操作
        error: function () {
            console.log("失败");
        },
        success: function (data) {
            if (data.code === 100) {
                hint(data.msg);
            }
            if (data.code === 200) {
                hint(data.msg);
            }
        },
    });
    request_data(GetThisUrl(), getstatus);
}
